#include <iostream>
#include <string>
#include <sstream>  //字符串流：字符串输入流和字符串输出流
#include "intarray.h"
#include "inherit.h"
#include "head.h"

using namespace std;

#define TO_NUMBER(s, n) (istringstream(s) >> n)
#define TO_STRING(n) ((static_cast<ostringstream&>(ostringstream() << n)).str())

void string_sort(string a[], int len)
{
    for(int i=0; i<len; i++)
    {
        for(int j=i; j<len; j++)
        {
            if( a[i] > a[j] )
            {
                swap(a[i], a[j]);
            }
        }
    }
}

string string_add(string a[], int len)
{
    string ret = "";

    for(int i=0; i<len; i++)
    {
        ret += a[i] + "; ";
    }

    return ret;
}

/*循环右移*/
string operator >> (const string& s, unsigned int n)
{
    string ret = "";
    unsigned int pos = 0;

    n = n % s.length();
    pos = s.length() - n;

    ret = s.substr(pos);
    ret += s.substr(0,pos);

    return ret;
}

int fib()
{
    static int a0 = 0;
    static int a1 = 1;

    int ret = a1;

    a1 = a0 + a1;
    a0 = ret;

    return ret;
}


class test_value
{
    int mvalue;
public:
    /*构造函数也叫转换构造函数*/
    explicit test_value(int v)
    {
       mvalue = v;
    }

    int value() const   //const 对象只能调用const 成员函数
    {
        return mvalue;
    }

    /*一般工程不使用*/
    operator int ()//类型转换函数
    {
        return mvalue;
    }

    int toint ()//成员函数
    {
        return mvalue;
    }
};

bool operator && (const test_value& l, const test_value& r)
{
    return l.value() && r.value();
}

test_value func(test_value i)
{
    cout << "test_value func(test_value i): i.value = " << i.value() << endl;
    return i;
}

int main()
{
    test_value t0(0);
    test_value t1(1);

    int t = t1;//隐式类型转换函数
    cout << "t = " << t << endl;

    if( func(t0) && func(t1) )
    {
        cout << "Result is true!" << endl;
    }
    else
    {
        cout << "Result is fale!" << endl;
    }
    IntArray* a = IntArray::NewInstance(5);

    if( a != NULL )
    {
        IntArray& array = a->self();

        cout << "array.length() = " << array.length() << endl;

        array[0] = 1;

        for(int i=0; i<array.length(); i++)
        {
            cout << array[i] << endl;
        }
    }

    delete a;

    string sa[7] =
    {
        "Hello Word",
        "Good Luck",
        "C#",
        "Java",
        "C++",
        "Python",
        "TypeScript"
    };

    string_sort(sa, 7);

    for(int i=0; i<7; i++)
    {
        cout << sa[i] << endl;
    }

    cout << endl;

    cout << string_add(sa, 7) << endl;

    cout << endl;

    /*string -> 数字*/
    //istringstream iss("123.56");
    double num = 0;

    //if( iss >> num )
    if( TO_NUMBER("123.56", num) )
    {
        cout << num << endl;
    }
    else
    {
        cout << "String Stream Error!" <<  endl;
    }

    cout << endl;

    /*数字 -> string*/
//    ostringstream oss;
//    oss << 543 << '.' << 21;
//    string s = oss.str();

    cout << TO_STRING(543.21) << endl;

    string s = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";

    string r = (s>>3);
    cout << r << endl;

    inherit_main();

    class_mode_test();

    func_mode_test();

    exception_handle_test();

    Exception_class_test();

    return 0;
}


